package xf.tools.popu;

import java.util.ArrayList;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.OnWheelClickedListener;
import kankan.wheel.widget.OnWheelScrollListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.AbstractWheelTextAdapter;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.application.EmmApplication;

public class SelectPicPopupWindow_Test extends PopupWindow implements OnWheelChangedListener, OnWheelClickedListener {

	private View mMenuView;
	private WheelView pickerView_type, pickerView, pickerView_language;
	private TextView order_yes, order_no;
	private String language_text = null, type_text = null, type_text_one = null;

	private ArrayList<City_provinceCityDistrict.ProvinceCityDistrict> city_provinceCityDistricts_one;
	Activity contexts;

	private ArrayList<String> language, type, type_one;

	private ArrayList<String> language_id, type_id, type_one_id;
	private Button imageView1;

	private int pickerView_type_Subscript, pickerView_language_Subscript, pickerView_Subscript;
	private int maxsize = 24;
	private int minsize = 14;

	private AddressTextAdapter provinceAdapter, provinceAdapter_two, provinceAdapter_three;

	private String areaId_one, areaId_two, areaId_three;

	@SuppressWarnings("deprecation")
	public SelectPicPopupWindow_Test(Activity context, OnClickListener clickListener) {
		super(context);
		EmmApplication.disKey(context);
		contexts = context;
		//
		city_provinceCityDistricts_one = EmmApplication.city_provinceCityDistricts;
		if (null == city_provinceCityDistricts_one) {
			EmmApplication.initJsonData(contexts);
			city_provinceCityDistricts_one = EmmApplication.city_provinceCityDistricts;
		}

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.city_popu_activity, null);
		pickerView_language = (WheelView) mMenuView.findViewById(R.id.pickerView_language);
		pickerView_type = (WheelView) mMenuView.findViewById(R.id.pickerView_type);
		pickerView = (WheelView) mMenuView.findViewById(R.id.pickerView);
		imageView1 = (Button) mMenuView.findViewById(R.id.imageView1);
		imageView1.setOnClickListener(clickListener);
		// 添加change事件
		pickerView_language.addChangingListener(this);
		pickerView_type.addChangingListener(this);
		pickerView.addChangingListener(this);
		Picker();
		// 设置按钮监听
		// 设置SelectPicPopupWindow的View
		this.setContentView(mMenuView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(LayoutParams.FILL_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimBottom);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0xb0000000);
		// 设置SelectPicPopupWindow弹出窗体的背景
		this.setBackgroundDrawable(dw);
		// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
		mMenuView.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				int height = mMenuView.findViewById(R.id.home_popview).getTop();
				int y = (int) event.getY();
				if (event.getAction() == MotionEvent.ACTION_UP) {
					if (y < height) {
						dismiss();
					}
				}
				return true;
			}
		});
	}

	/*
	 * 填充网络数据
	 */
	public void Picker() {
		language = new ArrayList<String>();
		type = new ArrayList<String>();
		type_one = new ArrayList<String>();
		language_id = new ArrayList<String>();
		type_id = new ArrayList<String>();
		type_one_id = new ArrayList<String>();
		// 遍历数据，分别为language,type,type_one,填充数据

		try {
			for (int i = 0; i < city_provinceCityDistricts_one.size(); i++) {
				language.add(city_provinceCityDistricts_one.get(i).getProvince());
				language_id.add(city_provinceCityDistricts_one.get(i).getProvinceId());
			}
			System.out.println("language is " + language.size() / 2);
			System.out.println("language is ys  " + language.get(language.size() / 2));

			for (int k = 0; k < city_provinceCityDistricts_one.get(0).getCities().size(); k++) {
				type.add(city_provinceCityDistricts_one.get(0).getCities().get(k).getCity());
				type_id.add(city_provinceCityDistricts_one.get(0).getCities().get(k).getCityId());
			}

			for (int h = 0; h < city_provinceCityDistricts_one.get(0).getCities().get(0).getDistricts().size(); h++) {
				type_one.add(city_provinceCityDistricts_one.get(0).getCities().get(0).getDistricts().get(h).getDistrict());
				type_one_id.add(city_provinceCityDistricts_one.get(0).getCities().get(0).getDistricts().get(h).getDistrictId());
			}

			provinceAdapter = new AddressTextAdapter(contexts, language, 0, maxsize, minsize);
			pickerView_language.setViewAdapter(provinceAdapter);

			provinceAdapter_two = new AddressTextAdapter(contexts, type, 0, maxsize, minsize);
			pickerView_type.setViewAdapter(provinceAdapter_two);

			provinceAdapter_three = new AddressTextAdapter(contexts, type_one, 0, maxsize, minsize);
			pickerView.setViewAdapter(provinceAdapter_three);

			language_text = language.get(0);
			type_text = type.get(0);
			type_text_one = type_one.get(0);

			EmmApplication.provinceId = language_id.get(0);
			EmmApplication.cityId = type_id.get(0);
			EmmApplication.areaId = type_one_id.get(0);

			areaId_one = language_id.get(0);
			areaId_two = type_id.get(0);
			areaId_three = type_one_id.get(0);

			Log.w("地址选择:ID", "provinceId:" + EmmApplication.provinceId + "/cityId:" + EmmApplication.cityId + "/areaId:" + EmmApplication.areaId);

			// System.out.println(Addres_add_Activity.provinceId + " / " +
			// Addres_add_Activity.cityId + " / " + Addres_add_Activity.areaId);

		} catch (NullPointerException e) {
			type_text = null;
			type_text_one = null;
		}

		pickerView_language.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				Change_data(wheel);
			}
		});

		pickerView_type.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				Change_region(wheel);
			}
		});

		pickerView.addScrollingListener(new OnWheelScrollListener() {

			@Override
			public void onScrollingStarted(WheelView wheel) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScrollingFinished(WheelView wheel) {
				try {
					String currentText = (String) provinceAdapter_three.getItemText(wheel.getCurrentItem());
					type_text_one = currentText;
					setTextviewSize(currentText, provinceAdapter_three);
					for (int i = 0; i < city_provinceCityDistricts_one.size(); i++) {

						for (int j = 0; j < city_provinceCityDistricts_one.get(i).getCities().size(); j++) {

							if (city_provinceCityDistricts_one.get(i).getCities().get(j).getCity().equals(type_text)) {

								EmmApplication.cityId = city_provinceCityDistricts_one.get(i).getCities().get(j).getCityId();

								for (int j2 = 0; j2 < city_provinceCityDistricts_one.get(i).getCities().get(j).getDistricts().size(); j2++) {

									if (city_provinceCityDistricts_one.get(i).getCities().get(j).getDistricts().get(j2).getDistrict()
											.equals(currentText)) {
										EmmApplication.areaId = city_provinceCityDistricts_one.get(i).getCities().get(j).getDistricts()
												.get(j2).getDistrictId();

										areaId_three = city_provinceCityDistricts_one.get(i).getCities().get(j).getDistricts().get(j2)
												.getDistrictId();
									}
								}
							}
						}
					}
				} catch (IndexOutOfBoundsException e) {
					type_text_one = null;
				}
			}
		});
	}

	public String getLanguage() {
		return language_text;
	}

	public String getType() {
		return type_text;
	}

	public String getType_One() {
		return type_text_one;
	}

	public String getareaId_one() {
		return areaId_one;
	}

	public String getareaId_two() {
		return areaId_two;
	}

	public String getareaId_three() {
		return areaId_three;
	}

	@Override
	public void onItemClicked(WheelView wheel, int itemIndex) {
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		if (wheel == pickerView_language) {

			pickerView_language_Subscript = newValue;

			String currentText = (String) provinceAdapter.getItemText(wheel.getCurrentItem());
			setTextviewSize(currentText, provinceAdapter);
			Change_data(wheel);
		} else if (wheel == pickerView) {
			pickerView_Subscript = newValue;
			String currentText = (String) provinceAdapter_three.getItemText(wheel.getCurrentItem());
			setTextviewSize(currentText, provinceAdapter_three);

		} else if (wheel == pickerView_type) {
			pickerView_type_Subscript = newValue;
			String currentText = (String) provinceAdapter_two.getItemText(wheel.getCurrentItem());
			setTextviewSize(currentText, provinceAdapter_two);
			Change_region(wheel);
		}

	}

	private class AddressTextAdapter extends AbstractWheelTextAdapter {
		ArrayList<String> list;

		protected AddressTextAdapter(Context context, ArrayList<String> list, int currentItem, int maxsize, int minsize) {
			super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize);
			this.list = list;
			setItemTextResource(R.id.tempValue);
		}

		@Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
			View view = super.getItem(index, cachedView, parent);
			return view;
		}

		@Override
		public int getItemsCount() {
			return list.size();
		}

		@Override
		protected CharSequence getItemText(int index) {
			return list.get(index) + "";
		}
	}

	/**
	 * 设置字体大小
	 * 
	 * @param curriteItemText
	 * @param adapter
	 */
	public void setTextviewSize(String curriteItemText, AddressTextAdapter adapter) {
		ArrayList<View> arrayList = adapter.getTestViews();
		int size = arrayList.size();
		String currentText;
		for (int i = 0; i < size; i++) {
			TextView textvew = (TextView) arrayList.get(i);
			currentText = textvew.getText().toString();
			if (curriteItemText.equals(currentText)) {
				textvew.setTextSize(24);
			} else {
				textvew.setTextSize(14);
			}
		}
	}

	public void Change_data(WheelView wheel) {
		try {

			if (type != null || type_one != null) {
				type_one.clear();
				type.clear();
				type_one_id.clear();
				type_id.clear();
				type_text = null;
				type_text_one = null;

			}

			language_text = language.get(pickerView_language_Subscript);

			String currentText = (String) provinceAdapter.getItemText(wheel.getCurrentItem());
			setTextviewSize(currentText, provinceAdapter);

			System.out.println("language_text is " + language_text);

			for (int i = 0; i < city_provinceCityDistricts_one.size(); i++) {
				if (city_provinceCityDistricts_one.get(i).getProvince().equals(currentText)
						&& city_provinceCityDistricts_one.get(i).getCities().size() > 0) {

					for (int j = 0; j < city_provinceCityDistricts_one.get(i).getCities().size(); j++) {

						type.add(city_provinceCityDistricts_one.get(i).getCities().get(j).getCity());

						type_id.add(city_provinceCityDistricts_one.get(i).getCities().get(j).getCityId());
					}

					for (int h = 0; h < city_provinceCityDistricts_one.get(i).getCities().get(0).getDistricts().size(); h++) {
						type_one.add(city_provinceCityDistricts_one.get(i).getCities().get(0).getDistricts().get(h).getDistrict());
						type_one_id.add(city_provinceCityDistricts_one.get(i).getCities().get(0).getDistricts().get(h).getDistrictId());
					}

					EmmApplication.provinceId = city_provinceCityDistricts_one.get(i).getProvinceId();
					EmmApplication.cityId = type_id.get(0);
					EmmApplication.areaId = type_one_id.get(0);

					areaId_one = city_provinceCityDistricts_one.get(i).getProvinceId();
					areaId_two = type_id.get(0);
					areaId_three = type_one_id.get(0);

					Log.w("ABAB:", EmmApplication.provinceId + " / " + EmmApplication.cityId + " / " + EmmApplication.areaId);
					type_text = type.get(0);
					type_text_one = type_one.get(0);
				}

				if (type != null) {

					provinceAdapter_two = new AddressTextAdapter(contexts, type, 0, maxsize, minsize);
					pickerView_type.setViewAdapter(provinceAdapter_two);
					pickerView_type.setCurrentItem(0);
				}

				if (type_one != null) {
					provinceAdapter_three = new AddressTextAdapter(contexts, type_one, 0, maxsize, minsize);
					pickerView.setViewAdapter(provinceAdapter_three);
					pickerView.setCurrentItem(0);
				}

			}

		} catch (IndexOutOfBoundsException e) {
			type_text = null;
			type_text_one = null;

			if (type != null) {
				provinceAdapter_two = new AddressTextAdapter(contexts, type, 0, maxsize, minsize);
				pickerView_type.setViewAdapter(provinceAdapter_two);
				pickerView_type.setCurrentItem(0);
			} else {
				type = null;
				type = new ArrayList<String>();
				type.add("");
				provinceAdapter_two = new AddressTextAdapter(contexts, type, 0, maxsize, minsize);
				pickerView_type.setViewAdapter(provinceAdapter_two);
				pickerView_type.setCurrentItem(0);
			}

			if (type_one != null) {
				provinceAdapter_three = new AddressTextAdapter(contexts, type_one, 0, maxsize, minsize);
				pickerView.setViewAdapter(provinceAdapter_three);
				pickerView.setCurrentItem(0);
			} else {
				type_one = null;
				type_one = new ArrayList<String>();
				type_one.add("");
				provinceAdapter_three = new AddressTextAdapter(contexts, type_one, 0, maxsize, minsize);
				pickerView.setViewAdapter(provinceAdapter_three);
				pickerView.setCurrentItem(0);
			}

		}
	}

	public void Change_region(WheelView wheel) {
		try {
			if (type_one != null) {
				type_one.clear();
				type_one_id.clear();
			}
			String currentText = (String) provinceAdapter_two.getItemText(wheel.getCurrentItem());
			setTextviewSize(currentText, provinceAdapter_two);
			type_text = currentText;
			for (int i = 0; i < city_provinceCityDistricts_one.size(); i++) {

				for (int j = 0; j < city_provinceCityDistricts_one.get(i).getCities().size(); j++) {

					if (city_provinceCityDistricts_one.get(i).getCities().get(j).getCity().equals(currentText)
							&& city_provinceCityDistricts_one.get(i).getProvince().equals(getLanguage())) {

						EmmApplication.cityId = city_provinceCityDistricts_one.get(i).getCities().get(j).getCityId();

						for (int j2 = 0; j2 < city_provinceCityDistricts_one.get(i).getCities().get(j).getDistricts().size(); j2++) {

							type_one.add(city_provinceCityDistricts_one.get(i).getCities().get(j).getDistricts().get(j2).getDistrict());

							type_one_id
									.add(city_provinceCityDistricts_one.get(i).getCities().get(j).getDistricts().get(j2).getDistrictId());

							type_text_one = type_one.get(0);

						}
						EmmApplication.areaId = type_one_id.get(0);
						areaId_three = type_one_id.get(0);
						Log.w("CDCD:", EmmApplication.provinceId + " / " + EmmApplication.cityId + " / " + EmmApplication.areaId);
					}

				}

			}
			provinceAdapter_three = new AddressTextAdapter(contexts, type_one, 0, maxsize, minsize);
			pickerView.setViewAdapter(provinceAdapter_three);
			pickerView.setCurrentItem(0);
		} catch (IndexOutOfBoundsException e) {
			type_text = null;
			type_text_one = null;
			type_one = null;
			type_one = new ArrayList<String>();
			type_one.add("");
			provinceAdapter_three = new AddressTextAdapter(contexts, type_one, 0, maxsize, minsize);
			pickerView.setViewAdapter(provinceAdapter_three);
			pickerView.setCurrentItem(0);
		}

	}

}
